package com.yonyou.rabbitmq.receiver.basic.routing;
import com.rabbitmq.client.*;
import com.yonyou.rabbitmq.utils.RabbitmqUtils;

import java.io.IOException;

public class Consumer2 {
    public static void main(String[] args) {

        try {
            //获取连接
            Connection connection = RabbitmqUtils.getConnection();
            if (connection != null) {
                //获取通道
                Channel channel = connection.createChannel();
                //声明交换机与交换机类型
                channel.exchangeDeclare("logs_direct","direct");
                //获取临时队列
                String queue = channel.queueDeclare().getQueue();
                //基于route key绑定队列与交换机
                channel.queueBind(queue,"logs_direct","info");
                channel.queueBind(queue,"logs_direct","error");
                channel.queueBind(queue,"logs_direct","warning");

                //消费消息
                DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
                    @Override
                    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                        System.out.println("Consumer2:"+new String(body));
                    }
                };
                channel.basicConsume(queue,true,defaultConsumer);
            }
        } catch (IOException e) {
            System.out.println("消费者消费消息完成......");
            e.printStackTrace();
        }
    }
}
